//
//  BTSaleCategoryVC.m
//  BaxTer
//
//  Created by wsj2015 on 16/10/14.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "BTSaleCategoryVC.h"
#import "TitleItemCell.h"
#import "SchoolManagerSectionHeader.h"
#import "XLPlainFlowLayout.h"
#import "BTSaleListVC.h"
#import "CWQ_historySearchVC.h"
#import "UICollectionView+ARDynamicCacheHeightLayoutCell.h"
@interface BTSaleCategoryVC ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation BTSaleCategoryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
- (void)configUI{
    UIBarButtonItem *right2 =[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"搜索_icon.png"] style:0 target:self action:@selector(toSearch)];
    
    self.navigationItem.rightBarButtonItem = right2;
    
    
    self.title = @"销售";
    
    XLPlainFlowLayout *layout =(XLPlainFlowLayout *)  self.collectionView.collectionViewLayout;
    
    CGFloat space = 20;
    layout.sectionInset =UIEdgeInsetsMake(space, space, space, space);
    layout.minimumInteritemSpacing = 8 ;
    layout.minimumLineSpacing = 20;
    CGFloat f = kScreenFactor;
    layout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - 20 * 2 -space * 2) /3.0, 33 * f);
    [self.collectionView registerNib:[UINib nibWithNibName:@"TitleItemCell" bundle:nil] forCellWithReuseIdentifier:@"TitleItemCell"];
    
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"SchoolManagerSectionHeader" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SchoolManagerSectionHeader"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    
    [self getSaleCouseFlags];
    
    
}


- (void)toSearch{
   
    CWQ_historySearchVC *vc =[CWQ_historySearchVC new];
    vc.searchType  =CWQ_historySearchVCType_Course_Sale_all;
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)getSaleCouseFlags{
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    
   
    //wsjtest
    
    weakSelf_define;
    self.collectionView.loading = YES;
    
    [[BTNetworkManager sharedMg] POST:kSaleCourseFlags parameters:params success:^(NSDictionary *responseObject) {
        weakSelf.collectionView.loading = NO;
      
        if ([responseObject[@"success"] boolValue]) {
         
//            "flagName": "销售技巧",
//            "parentID": 4,
//            "status": 1,
//            "createDate": 1476773469000,
//            "updateDate": null,
//            "isDelete": 1,
//            "iconUrl": "http://img4.imgtn.bdimg.com/it/u=766424822,3030155408&fm=21&gp=0.jpg",
//            "courseflagList": [],
//            "id": 40
            NSArray<NSDictionary *> *arr =  responseObject[@"data"];
            if ([arr isKindOfClass:[NSArray class]]) {
                 [weakSelf.dataArray addObjectsFromArray:arr];
            }
           
            [weakSelf.collectionView reloadData];
        }
        
    } failure:^(NSString *err) {
        weakSelf.collectionView.loading = NO;
       
    }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.dataArray.count;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSDictionary *dict = self.dataArray[section];
    NSArray *arr = dict[@"courseflagList"];
    if (!arr || ![arr isKindOfClass:[NSArray class]]) {
        return 0;
    }
    return arr.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TitleItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TitleItemCell" forIndexPath:indexPath];
    
    NSDictionary *dict = self.dataArray[indexPath.section];
    NSArray *arr = dict[@"courseflagList"];
    NSDictionary *subDict = arr[indexPath.row];
    cell.titleL.text = subDict[@"flagName"];
    
    //    cell.titleL.text = str;
    return cell;
    
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    //    SchoolManagerSectionHeader
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        SchoolManagerSectionHeader *header = (SchoolManagerSectionHeader *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SchoolManagerSectionHeader" forIndexPath:indexPath];

        NSDictionary *dict = self.dataArray[indexPath.section];
        header.titleL.text = dict[@"flagName"];
        header.titleL.textColor = [UIColor grayColor];
        return header;
    }else{
        UICollectionReusableView *footer =[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        footer.backgroundColor = [UIColor clearColor];
        
        return footer;
        
    }
    return nil;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
     CGFloat f = kScreenFactor;
  
    
    return CGSizeMake((kScreenWidth -3*20*f)/3, 33);

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    CGFloat f =  kScreenFactor;
    return CGSizeMake(kScreenWidth, 30 * f);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if (section == 3) {
        return CGSizeMake(kScreenWidth, .1);
    }
    return CGSizeMake(kScreenWidth, 1);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = self.dataArray[indexPath.section];
    NSArray *arr = dict[@"courseflagList"];
    NSDictionary *subDict = arr[indexPath.row];
    BTSaleListVC *vc = [BTSaleListVC new] ;
    vc.courseflagId = [subDict[@"id"] integerValue];
    vc.title = subDict[@"flagName"];
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
@end
